package org.yanghaokun.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yanghaokun.basic.exception.BusinessException;
import org.yanghaokun.basic.service.impl.BaseServiceImpl;
import org.yanghaokun.org.domain.Employee;
import org.yanghaokun.org.domain.Shop;
import org.yanghaokun.org.mapper.EmployeeMapper;
import org.yanghaokun.org.mapper.ShopMapper;
import org.yanghaokun.org.service.IEmployeeService;
import org.yanghaokun.org.service.IShopService;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void bulkDel(List<Long> ids) {
        shopMapper.bulkDel(ids);
    }

    @Override
    public void stttleIn(Shop shop) {
        //对用户进行校验，因为前台我们把店铺和员工的字段全部增和在一起了所以现在在分开
        //判断是否本来就有，有的话就不能成功
        Employee admin = shop.getAdmin();
        //根据用户需要上传的店铺信息很数据库对比看是否已经有了
        Employee employee = employeeMapper.findUser(admin);
        if (employee != null) {
            throw new BusinessException("已经入驻，请直接登录");
        }
        //不存在直接开始店铺入驻
        //保存管理人员信息并放回自增id
        employeeService.add(admin);
        //给shop添加admin.id
        shopMapper.save(shop);

        //管理员和店铺互相关联
        admin.setShop(shop);
        employeeMapper.update(admin);
    }
}
